var dataList;

var viewModel = new function () {

    this.cart = ko.observableArray([]);
    this.existLine = ko.computed(function () {
        if (this.cart().length > 0) return true;
        return false;
    }, this);

    /////////////////////////////////////// Init data /////////////////////////////////////////////
    var i = 0;
    // Class to represent a row in the table products
    function ProductItem(_Type, _Price, _Vendor, _Warranty, _MadeIn) {
        this.Id = i;
        this.Name = "Sản phẩm " + i;
        this.ProductCode = "PAKER_" + i * 645;
        this.ImagePath = "images/products/" + i + ".PNG";
        this.Type = _Type;
        this.Price = _Price;
        this.PriceFormat = formatCurrency("#.##0,#", _Price);
        
        this.Vendor = _Vendor;
        this.Warranty = _Warranty;
        this.MadeIn = _MadeIn;
        i++;
    }

    this.productList = ko.observableArray();
    this.productList.push(new ProductItem("ButBi", 6900, "PARKER", "2", "Italy")); //0
    this.productList.push(new ProductItem("ButDa", 200, "WATERMAN", "2", "Japan")); //1
    this.productList.push(new ProductItem("ButMay", 2500, "PARKER", "2", "Italy")); //2
    this.productList.push(new ProductItem("ButBi", 73000, "PARKER", "2", "Italy")); //3
    this.productList.push(new ProductItem("ButBi", 131100, "WATERMAN", "2", "Japan")); //4
    this.productList.push(new ProductItem("ButMay", 4500, "PARKER", "2", "Italy")); //5
    this.productList.push(new ProductItem("ButBi", 500, "PARKER", "2", "Japan")); //6
    this.productList.push(new ProductItem("ButMay", 9500, "WATERMAN", "2", "Japan")); //7
    this.productList.push(new ProductItem("ButDa", 10500, "PARKER", "2", "Italy")); //8
    this.productList.push(new ProductItem("ButDa", 7600, "WATERMAN", "2", "Japan")); //9
    this.productList.push(new ProductItem("ButMay", 18500, "PARKER", "2", "Korea")); //10
    this.productList.push(new ProductItem("ButBi", 9500, "WATERMAN", "2", "Korea")); //11
    this.productList.push(new ProductItem("ButDa", 10000, "PARKER", "2", "Italy")); //12
    this.productList.push(new ProductItem("ButMay", 54500, "WATERMAN", "2", "Italy")); //13
    this.productList.push(new ProductItem("ButBi", 21000, "PARKER", "2", "Korea")); //14
    this.productList.push(new ProductItem("ButMay", 37000, "PARKER", "2", "Japan")); //15


    var i = 0;
    // Class to represent a row in the table products
    function CartItem(_Product) {
        var self = this;
        self.Id = (i + 1);
        writeLog(_Product);
        self.Product = ko.observable(_Product);
        self.Quantity = ko.observable(1);
        self.Subtotal = ko.computed(function () {
            //writeLog(self.Quantity());
            //writeLog(self.Product().Price);
            return (self.Product().Price * self.Quantity());
        });
        self.SubtotalFormat = ko.computed(function () {
            //var subtotal = 
            return formatCurrency("#.##0,#", parseFloat(self.Subtotal()));
        });
        i++;
    }
    //writeLog(this.productList());
    this.cart.push(new CartItem(this.productList()[5]));
    this.cart.push(new CartItem(this.productList()[6]));
    this.cart.push(new CartItem(this.productList()[7]));

    $("#productNum").text(this.cart().length);

    this.grandTotal = ko.computed(function () {
        var total = 0;
        var _cart = this.cart();
        writeLog(_cart);
        $.each(this.cart(), function () { total += this.Subtotal() })
        return formatCurrency("#.##0,#", total);
    }, this);



};

ko.applyBindings(viewModel, document.getElementById('viewOrders'));

function deletePermanent() {
        writeLog("DeletePermanent");
        var itemId = $('#itemId').val();
        var list = viewModel.cart();
        viewModel.cart([]);
		for (var i = 0; i < list.length; i++) {
			if (list[i].Id == itemId) {
				list.splice(i, 1);
			}
		}
		viewModel.cart(list);
		$("#productNum").text(viewModel.cart().length);
		closePermanentPopup();
	};

function showDeleteConfirm(cartId) {
    $('#itemId').val(cartId);
    $('#deletePermanent').showPopup({
		width: 400
	});
	$('#fancybox-overlay').show();
    writeLog("deleteShoppingCartItem():" + cartId);
}
function closePermanentPopup() {
	$('#deletePermanent').hide();
	$('#fancybox-overlay').hide();
	$(".fancybox-wrap").hide();
}
